home *** CD-ROM | disk | FTP | other *** search
/ The Best of MacTutor - S…e Code for Volumes 1 to 5 / The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin / Source Code / #10 (Jul 86) / window demo source / wind.c < prev    next >
C/C++ Source or Header  |  1986-06-09  |  4KB  |  191 lines

  1. /* window manager demonstration 
  2.  * base on program in 
  3.  * Using Macintosh Toolbox with C
  4.  * page 70
  5.  */
  6.  
  7.  /* Here are our include files */
  8.  
  9.  #include    "abc.h"        /* Our own defines */
  10.  #include    "Events.h"    /* also includes Macdefs.h */
  11.  #include    "Window.h"    /* also includes Quickdraw.h, which 
  12.                                       in turn requires M68KLIB.D  */
  13.  
  14.  /* Here are our defines */
  15.  
  16.  #define        Screen    QD->screenBits.bounds
  17.  #define        charCodeMask    0x000000FF
  18.  
  19.  /* Here are our Global variables */
  20.  
  21.  WindowPtr        theWindow;
  22.  WindowRecord    windowRec;
  23.  Rect                dragbound;
  24.  Rect                limitRect;
  25.  
  26. main()
  27. {
  28.     InitWindows();
  29.     InitCursor();
  30.     FlushEvents(everyEvent);
  31.     
  32.     /* Initialize our global variables */
  33.     
  34.     theWindow = Nil;        /*indicates no window */
  35.     SetRect(&dragbound,
  36.                 Screen.left + 4,
  37.                 Screen.top + 24,
  38.                Screen.right - 4,
  39.                 Screen.bottom - 4);
  40.     SetRect(&limitRect,60,40,
  41.                 Screen.right - Screen.left - 4,
  42.                 Screen.bottom - Screen.top - 24);
  43.                 
  44.     dowindow();                /* make new window */  
  45.     eventloop();            /* check for events */
  46. }
  47.  
  48. dowindow()
  49. {
  50.     char        *title;            /* first title for window */
  51.     Rect        boundsRect;
  52.                 
  53.     if (not theWindow)                /* if no window exists, make one */
  54.             {
  55.             title = "ABC Window";
  56.             SetRect(&boundsRect,50,50,300,150);
  57.             theWindow = NewWindow(windowRec, &boundsRect,
  58.                                 CtoPstr(title),True,documentProc,
  59.                                 (WindowPtr) -1, True, 0);
  60.             DrawGrowIcon(theWindow);
  61.             PtoCstr(title);
  62.             }
  63. }
  64.  
  65. eventloop()
  66. {
  67.     EventRecord        theEvent;
  68.  
  69.     while(True)
  70.         if (GetNextEvent(everyEvent,&theEvent))
  71.             switch(theEvent.what)                    
  72.                 {                        /* only check key and */
  73.                 case keyDown:         /* mouse down events */
  74.                     dokey(&theEvent);
  75.                     break;
  76.                 case mouseDown:
  77.                     domouse(&theEvent);
  78.                     break;
  79.                 default:
  80.                     break;
  81.                 }
  82. }
  83.  
  84. dokey(er)
  85.     EventRecord        *er;
  86. {
  87.     char        c;                    /* character from message */
  88.     char        *title2;            /* second title for window */
  89.     
  90.     if (not(er->modifiers & cmdKey))        /* only pay attention to cmd keys */
  91.         return;                                    /* if command key is down */
  92.         
  93.     c = er->message & charCodeMask;        /* extract character, lower 8 bits */
  94.     
  95.     if (c equals 'q' or c equals 'Q')    /* 'q' quits program */
  96.         ExitToShell();    
  97.     
  98.     if (not theWindow)
  99.         {
  100.             if (c equals 'm' or c equals 'M')
  101.                 {
  102.                 dowindow();
  103.                 return;
  104.                 }
  105.             else
  106.                 {
  107.                 SysBeep(1);
  108.                 return;
  109.                 }
  110.         }
  111.         
  112.         /* Have a window, so try commands */
  113.         
  114.         switch (c)
  115.             {
  116.             case 'x':
  117.             case 'X':
  118.                 CloseWindow(theWindow);
  119.                 theWindow = Nil;
  120.                 break;
  121.             case 's':
  122.             case 'S':
  123.                 ShowWindow(theWindow);
  124.                 DrawGrowIcon(theWindow);
  125.                 break;
  126.             case 'h':
  127.             case 'H':
  128.                 HideWindow(theWindow);
  129.                 break;
  130.             case 't':
  131.             case 'T':
  132.                 title2 = "A Different Title";
  133.                 SetWTitle(theWindow, CtoPstr(title2));
  134.                 PtoCstr(title2);
  135.                 break;
  136.             default:
  137.                 SysBeep(1);
  138.                 break;
  139.             }
  140. }
  141.  
  142.  
  143. domouse(er)
  144.     EventRecord    *er;
  145. {
  146.     short            windowcode;
  147.     WindowPtr    whichWindow;
  148.     short            ingo;
  149.     long            size;
  150.     
  151.     windowcode = FindWindow(&er->where, &whichWindow);
  152.     switch (windowcode)
  153.         {
  154.         case inDesk:
  155.             if (theWindow notequal 0)
  156.                 {
  157.                 HiliteWindow(theWindow, False);
  158.                 DrawGrowIcon(theWindow);
  159.                 }
  160.             else
  161.                 ExitToShell();    /* allow an exit if no window */
  162.             break;
  163.         case inMenuBar:
  164.             SysBeep(1);
  165.             break;
  166.         case inSysWindow:
  167.             SysBeep(1);
  168.             break;
  169.         case inContent:
  170.             HiliteWindow(whichWindow,True);
  171.             DrawGrowIcon(theWindow);
  172.             break;
  173.         case inDrag:
  174.             DragWindow(whichWindow, &er->where, &dragbound);
  175.             DrawGrowIcon(theWindow);
  176.             break;
  177.         case inGrow:
  178.             /* not included this month */
  179.             break;
  180.         case inGoAway:
  181.             ingo = TrackGoAway(whichWindow,&er->where);
  182.             if (ingo)
  183.                 {
  184.                 CloseWindow(whichWindow);
  185.                 theWindow = Nil;
  186.                 }
  187.             break;
  188.         }
  189. }
  190.  
  191.